from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import User, UserProfile, Permission, RolePermission, ResourcePermission, UserRole


@admin.register(User)
class CustomUserAdmin(UserAdmin):
    """自定义用户管理界面"""
    
    list_display = ['username', 'email', 'display_name', 'role', 'is_active', 'created_at']
    list_filter = ['role', 'is_active', 'is_staff', 'is_superuser', 'created_at']
    search_fields = ['username', 'email', 'display_name']
    ordering = ['-created_at']
    
    fieldsets = (
        (None, {'fields': ('username', 'password')}),
        ('个人信息', {'fields': ('display_name', 'email', 'bio', 'avatar_url')}),
        ('权限', {'fields': ('role', 'is_active', 'is_staff', 'is_superuser', 'groups', 'user_permissions')}),
        ('重要日期', {'fields': ('last_login', 'date_joined', 'created_at', 'updated_at')}),
    )
    
    readonly_fields = ['created_at', 'updated_at']
    
    def get_queryset(self, request):
        return super().get_queryset(request).select_related('profile')


@admin.register(UserProfile)
class UserProfileAdmin(admin.ModelAdmin):
    """用户资料管理界面"""
    
    list_display = ['user', 'phone', 'location', 'company', 'article_count', 'comment_count']
    list_filter = ['location', 'company']
    search_fields = ['user__username', 'user__email', 'phone', 'location', 'company']
    
    fieldsets = (
        ('基本信息', {'fields': ('user', 'phone', 'website', 'location')}),
        ('职业信息', {'fields': ('company', 'job_title')}),
        ('社交媒体', {'fields': ('github_url', 'twitter_url', 'linkedin_url')}),
        ('统计信息', {'fields': ('article_count', 'comment_count', 'like_count', 'view_count')}),
        ('通知设置', {'fields': ('email_notifications', 'comment_notifications')}),
        ('时间信息', {'fields': ('created_at', 'updated_at')}),
    )
    
    readonly_fields = ['created_at', 'updated_at']


@admin.register(Permission)
class PermissionAdmin(admin.ModelAdmin):
    """权限管理界面"""
    
    list_display = ['name', 'code', 'category', 'created_at']
    list_filter = ['category']
    search_fields = ['name', 'code', 'description']
    ordering = ['category', 'name']


@admin.register(RolePermission)
class RolePermissionAdmin(admin.ModelAdmin):
    """角色权限管理界面"""
    
    list_display = ['role', 'permission', 'created_at']
    list_filter = ['role']
    search_fields = ['permission__name', 'permission__code']


@admin.register(ResourcePermission)
class ResourcePermissionAdmin(admin.ModelAdmin):
    """资源权限管理界面"""
    
    list_display = ['resource_type', 'resource_id', 'permission_type', 'user', 'role', 'expires_at']
    list_filter = ['resource_type', 'permission_type', 'role']
    search_fields = ['resource_type', 'resource_id']
    
    fieldsets = (
        ('资源信息', {'fields': ('resource_type', 'resource_id', 'permission_type')}),
        ('授权对象', {'fields': ('user', 'role')}),
        ('授权信息', {'fields': ('granted_by', 'expires_at', 'created_at')}),
    )
    
    readonly_fields = ['created_at']